#include "FrameTools.h"
#include "Utility\Log.h"
#include "Utility\StrFun.h"
#include "Utility\SysFun.h"
#include "SysResources\Lock.h"
#include "SysResources\GtGui.h"
#include "SysResources\Trigger.h"
#include "IPC\InterProcessCommunication .h"
#include "IPC\DisposeMessage.h"
#include "ClientApi\DataCenterClient.h"
#include "ContainerManager\ProcessShrMem.h"
#include "ContainerManager\Queue.h"
#include "ContainerManager\QueueOperation.h"
#include "ContainerManager\ShrMemQueue.h"
#include "ThreadPool\Thread.h"
#include "ThreadPool\ThreadPool.h"
#include "ThreadPool\ThreadPoolManager.h"
#include "Paket\Data.h"
#include "Paket\PacketJson.h"

IFrameTools * glbFrameTools = nullptr;

const bool CFrameTools::LoadInstance(IKernel * ptrKernel)
{
	bool bRetVal = false;	
	bool bFlg = strstr(ptrKernel->ConfigFileName(), "DataCenterInfo") != nullptr ? true : false;
	if (!ptrKernel) goto __end;
	m_ptrKernel = ptrKernel;	
	m_ptrKernel->AddInstance(CStrFun::Instance());
	m_ptrKernel->AddInstance(CSysFun::Instance());
	m_ptrKernel->AddInstance(CLog::Instance());
	m_ptrKernel->AddInstance(CLockSet::Instance());
	m_ptrKernel->AddInstance(CGtGui::Instance());
	m_ptrKernel->AddInstance(CThreadLock::Instance());
	m_ptrKernel->AddInstance(CProcessLock::Instance());
	m_ptrKernel->AddInstance(CGtSemaphore::Instance());
	m_ptrKernel->AddInstance(CGtTimer::Instance());
	m_ptrKernel->AddInstance(CInterProcessCommunication::Instance());
	m_ptrKernel->AddInstance(CDisposeSendData::Instance());
	m_ptrKernel->AddInstance(CDisposeReceiveData::Instance());
	m_ptrKernel->AddInstance(CDataCenterDisposeData::Instance());
	m_ptrKernel->AddInstance(CClientDisposeData::Instance());
	if (!bFlg) m_ptrKernel->AddInstance(CDataCenterClient::Instance());
	if (!bFlg) m_ptrKernel->AddInstance(CCheckDataCentre::Instance());
	m_ptrKernel->AddInstance(CQueue::Instance());
	m_ptrKernel->AddInstance(CShrMemQueue::Instance());
	m_ptrKernel->AddInstance(CProcessShrMem::Instance());
	m_ptrKernel->AddInstance(CQueueOperation::Instance());
	m_ptrKernel->AddInstance(CThreadPoolManager::Instance());
	m_ptrKernel->AddInstance(CThreadPool::Instance());
	m_ptrKernel->AddInstance(CThread::Instance());
	m_ptrKernel->AddInstance(CPacketJson::Instance());
	m_ptrKernel->AddInstance(CData::Instance());
	bRetVal = InitInstance(bFlg);
__end:
	return bRetVal;
}

const bool CFrameTools::InitInstance(bool bFlg)
{
	bool bRetVal = false;
	m_ptrLog = Instance<ILog>("CLog", nullptr);
	m_ptrSysFun = Instance<ISysFun>("CSysFun", nullptr);
	m_ptrStrFun = Instance<IStrFun>("CStrFun", nullptr);
	m_ptrLockSet = Instance<ILockSet>("CLockSet", nullptr);		                                        
	m_ptrGui = Instance<IGui>("CGtGui", nullptr);	
	if (!bFlg) m_ptrClient = Instance<IClient>("CDataCenterClient", nullptr);
	m_ptrQueueOperation = Instance<IQueueOperation>("CQueueOperation", nullptr);
	m_ptrInterProcessCommunication = Instance<IInterProcessCommunication>("CInterProcessCommunication", nullptr);
	m_ptrThreadManager = Instance<IThreadManager>("CThreadPoolManager", nullptr);
	bRetVal = m_ptrLog != nullptr &&
		m_ptrSysFun != nullptr &&
		m_ptrStrFun != nullptr &&
		m_ptrLockSet != nullptr &&
		m_ptrGui != nullptr &&
		(!bFlg) ? m_ptrClient != nullptr : true &&
		m_ptrQueueOperation != nullptr &&
		m_ptrInterProcessCommunication != nullptr &&
		m_ptrThreadManager != nullptr;
__end:
	return bRetVal;
}

ILog & CFrameTools::Log()
{
	return *m_ptrLog;
}

ISysFun & CFrameTools::SysFun()
{
	return *m_ptrSysFun;
}

IStrFun & CFrameTools::StrFun()
{
	return *m_ptrStrFun;
}

ILockSet & CFrameTools::LockSet()
{
	return *m_ptrLockSet;
}

IGui & CFrameTools::Gui()
{
	return *m_ptrGui;
}

IClient & CFrameTools::Client()
{
	return *m_ptrClient;
}

IQueueOperation & CFrameTools::QueueOperation()
{
	return *m_ptrQueueOperation;
}

IInterProcessCommunication & CFrameTools::IPC()
{
	return *m_ptrInterProcessCommunication;
}

IThreadManager & CFrameTools::ThreadManager()
{
	return *m_ptrThreadManager;
}

ITrigger * CFrameTools::CreateTimer(const char * pszName)
{
	return Instance<ITrigger>("CGtTimer", reinterpret_cast<const void *>(pszName));
}

ITrigger * CFrameTools::CreateSmper(const char * pszName)
{
	return Instance<ITrigger>("CGtSemaphore", reinterpret_cast<const void *>(pszName));
}

IData * CFrameTools::CreateData(const char * pszBufer, const int iLen)
{
	IData * ptrRetVal = Instance<IData>("CData", nullptr);
	if (!ptrRetVal->Parse(pszBufer, iLen))
	{
		delete ptrRetVal;
		ptrRetVal = nullptr;
	}
	return ptrRetVal;
}

IPacket * CFrameTools::CreateJsonPacket(const char * pszBufer, const int iLen)
{
	IPacket * ptrRetVal = Instance<IPacket>("CPacketJson", nullptr);
	if (!StrFun().IsEmpty(pszBufer) && !ptrRetVal->Parse(pszBufer, iLen))
	{
		delete ptrRetVal;
		ptrRetVal = nullptr;
	}
	return ptrRetVal;
}

ILock * CFrameTools::CreateThreadLock()
{
	return Instance<ILock>("CThreadLock", nullptr);;
}

ILock * CFrameTools::CreateProcessLock(const char * pszName)
{
	return Instance<ILock>("CProcessLock", reinterpret_cast<const void *>(pszName));;
}